<?php
/**
 * 定时任务
 * @author ***
 *
 */
class CrondAction extends HomeAction
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	public function index()
	{

	}
    /*
     * 加入购物车未购买 定时任务专用
     * 每天12点执行定时任务
     */
    public function time(){
        global $language_id;
        $now_time = time();
        $end_time = $now_time - 3600*12;//加入购物车后12小时发送邮件
        $start_time = $now_time - 3600*36;//加入购物车后12小时发送邮件
        
        $sql = "select cart_products.*,users.email from cart_products left join users on cart_products.user_id=users.id  where cart_products.user_id !=0 and cart_products.add_time>$start_time and cart_products.add_time<$end_time  group by cart_products.user_id ";
        $cart_product_list = M('')->query($sql);
        //echo M()->getLastSql();die;
        //var_dump($cart_product_list);die;
        $mailInstance = new EmailModel();
        $email_template = UserModel::get_email_template_info('cartProduct', $language_id);
            foreach($cart_product_list as $k=>$v){
                if($v['email']){
                    $email = $v['email'];
                    //$email_base64 = base64_encode($email);
                    //$sign  = MD5($email.C('pwd_key'));
                    //$unsubscribe_url = url('measure/unsubscribe','email='.$email_base64.'&sign='.$sign);
                    $user_name = '';
                    $user_info = UserModel::get_user_info($v['user_id']);
                    if($user_info && ($user_info['first_name'] && $user_info['last_name'])){
                        $user_name = $user_info['first_name'].' '.$user_info['last_name'];
                    }
                    $content = str_replace('{$user_name}',$user_name,$email_template['content']);
                    //$content = str_replace('{$unsubscribe_url}',$unsubscribe_url,$email_template['content']);
                    $mailInstance->sendMail( $email,$email_template['title'],  $content);
                }
            }
	   //回访邮件	
	   $this->orderReturnMail();	
       die;
    }

    //回访邮件
    public function orderReturnMail(){

        $now_time = time();        
        $start_time = strtotime('2015-03-01 00:00:00'); //早于这个时间点的订单不发送回访邮件
        $end_time   = $now_time - 3600*24*7;//七天之前发送的订单

        $where = array(
            'order_status'    => array('neq', OrderModel::ORDER_DEL),
            'shipping_status' => OrderModel::SHIPPING_SHIPPED,
            'is_return'       => 0,
            );
        $where['_string'] = 'shipping_time > '.$start_time.' AND shipping_time < '.$end_time;
        $orderList = M('orders')->where($where)->order('shipping_time asc')->select();
        //echo M()->getLastSql();
        //echo '<pre />';
        //print_r($orderList);
        $mailInstance = new EmailModel();
        foreach($orderList as $orderInfo){
            $email_template = UserModel::get_email_template_info('orderReturnMail', $orderInfo['language_id']);
            if($email_template){
                $content = $mailInstance->replaceMail($email_template['content'], $orderInfo);
                $title = $mailInstance->replaceMail($email_template['title']);
                $mailInstance->sendMail($orderInfo['delivery_email'], $title, $content);
            }
            //发送后修改is_return=1
            M('orders')->where(array('id' => $orderInfo['id']))->save(array('is_return'=>1));
        }   
    }

}
    

